<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="数据库-分页" tabindex="-1"><a class="header-anchor" href="#数据库-分页"><span>数据库：分页</span></a></h2>
<ul>
<li><a href="#introduction">介绍</a></li>
<li><a href="#basic-usage">基本用法</a>
<ul>
<li><a href="#paginating-query-builder-results">对查询构造器结果进行分页</a></li>
<li><a href="#paginating-eloquent-results">对 Eloquent 结果进行分页</a></li>
<li><a href="#cursor-pagination">游标分页</a></li>
<li><a href="#manually-creating-a-paginator">手动创建分页器</a></li>
<li><a href="#customizing-pagination-urls">自定义分页 URL</a></li>
</ul>
</li>
<li><a href="#displaying-pagination-results">显示分页结果</a>
<ul>
<li><a href="#adjusting-the-pagination-link-window">调整分页链接窗口</a></li>
<li><a href="#converting-results-to-json">将结果转换为 JSON</a></li>
</ul>
</li>
<li><a href="#customizing-the-pagination-view">自定义分页视图</a>
<ul>
<li><a href="#using-bootstrap">使用 Bootstrap</a></li>
</ul>
</li>
<li><a href="#paginator-instance-methods">分页器实例方法</a></li>
<li><a href="#cursor-paginator-instance-methods">游标分页器实例方法</a></li>
</ul>
<h2 id="介绍" tabindex="-1"><a class="header-anchor" href="#介绍"><span>介绍</span></a></h2>
<p>在其他框架中，分页可能非常痛苦，我们希望 Laravel 的分页方法像一股清新的空气。Laravel 的分页器集成了 <a href="https://learnku.com/docs/laravel/11.x/queriesmd" target="_blank" rel="noopener noreferrer">query builder</a> 和 <a href="https://learnku.com/docs/laravel/11.x/eloquentmd" target="_blank" rel="noopener noreferrer">Eloquent ORM</a> 并提供了方便、易于使用的无需任何配置的数据库记录分页。</p>
<p>默认情况下，由分页器生成的 HTML 与 <a href="https://tailwindcss.com/" target="_blank" rel="noopener noreferrer">Tailwind CSS 框架</a> 兼容；不过对 Bootstrap 的分页支持也是可用的。</p>
<h4 id="tailwind-jit" tabindex="-1"><a class="header-anchor" href="#tailwind-jit"><span>Tailwind JIT</span></a></h4>
<p>如果你使用 Laravel 的默认 Tailwind 视图和 Tailwind JIT 引擎，你应该确保你的应用程序的 <code v-pre>tailwind.config.js</code> 文件的 <code v-pre>content</code> 字段引用了 Laravel 的分页视图，这样它们的 Tailwind 类就不会被清除了：</p>
<div class="language-javascript line-numbers-mode" data-highlighter="prismjs" data-ext="js" data-title="js"><pre v-pre class="language-javascript"><code><span class="line"><span class="token literal-property property">content</span><span class="token operator">:</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string">'./resources/**/*.blade.php'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string">'./resources/**/*.js'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string">'./resources/**/*.vue'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string">'./vendor/laravel/framework/src/Illuminate/Pagination/resources/views/*.blade.php'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="基础用法" tabindex="-1"><a class="header-anchor" href="#基础用法"><span>基础用法</span></a></h2>
<h3 id="对查询构造器结果进行分页" tabindex="-1"><a class="header-anchor" href="#对查询构造器结果进行分页"><span>对查询构造器结果进行分页</span></a></h3>
<p>有几种方法可以对结果进行分页，最简单的方法是在 <a href="https://learnku.com/docs/laravel/11.x/queriesmd" target="_blank" rel="noopener noreferrer">query builder</a> 或 <a href="https://learnku.com/docs/laravel/11.x/eloquentnd" target="_blank" rel="noopener noreferrer">Eloquent query</a> 上使用 <code v-pre>paginate</code> 方法。<code v-pre>paginate</code> 方法根据用户查看的当前页面自动设置查询的「limit」和「offset」。默认情况下，通过 HTTP 请求中的 page 查询字符串参数的值检测当前页面，Laravel 会自动检测这个值，它也会自动插入到分页器生成的链接中。</p>
<p>在这个示例中，传给 <code v-pre>paginate</code> 方法的唯一参数就是你希望 “每页” 显示的条目数。在本例中，我们指定每页显示 <code v-pre>15</code> 个项目：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers<span class="token punctuation">\</span>Controller</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>DB</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>View<span class="token punctuation">\</span>View</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">UserController</span> <span class="token keyword">extends</span> <span class="token class-name">Controller</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 显示所有应用程序用户。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">index</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">View</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user.index'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'users'</span> <span class="token operator">=></span> <span class="token class-name static-context">DB</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">paginate</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="简单分页" tabindex="-1"><a class="header-anchor" href="#简单分页"><span>简单分页</span></a></h4>
<p><code v-pre>paginate</code> 方法在从数据库检索记录之前，先计算查询匹配的记录总数。这样做是为了让分页器知道总共有多少页记录。不过，如果你不打算显示总页数，那么计数查询就没有必要了。</p>
<p>因此，如果你只需要显示简单的 &quot;下一步 &quot;和 &quot;上一步 &quot;链接，您可以使用 <code v-pre>simplePaginate</code> 方法来执行一次高效的查询：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name static-context">DB</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">simplePaginate</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="分页集合结果" tabindex="-1"><a class="header-anchor" href="#分页集合结果"><span>分页集合结果</span></a></h3>
<p>您也可以对 <a href="https://learnku.com/docs/laravel/11.x/eloquent" target="_blank" rel="noopener noreferrer">Eloquent</a> 查询进行分页。在本例中，我们将分页显示 <code v-pre>App\Models\User</code> 模型，并指出我们计划每页显示 <code v-pre>15</code> 条记录。如您所见，该语法与查询生成器结果的分页几乎完全相同：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">paginate</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当然，你也可以在对查询设置其他约束条件（如 <code v-pre>where</code> 子句）后调用 <code v-pre>paginate</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'>'</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">paginate</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你也可以在 Eloquent ORM 分页中使用 <code v-pre>simplePaginate</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'>'</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">simplePaginate</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>同样，您可以使用 <code v-pre>cursorPaginate</code> 方法对 Eloquent 模型进行游标分页：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'>'</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">cursorPaginate</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="每页有多个-paginator-实例" tabindex="-1"><a class="header-anchor" href="#每页有多个-paginator-实例"><span>每页有多个 Paginator 实例</span></a></h4>
<p>有时你可能需要在应用程序呈现的单个屏幕上呈现两个单独的分页器。 但是，如果两个分页器实例都使用 <code v-pre>page</code> 查询字符串参数来存储当前页面，则两个分页器会发生冲突。要解决此冲突，您可以通过提供给 <code v-pre>paginate</code>、<code v-pre>simplePaginate</code> 和 <code v-pre>cursorPaginate</code> 方法的第三个参数传递你希望用于存储分页器当前页面的查询字符串参数的名称：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'>'</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">paginate</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token variable">$perPage</span> <span class="token operator">=</span> <span class="token number">15</span><span class="token punctuation">,</span> <span class="token variable">$columns</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'*'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token variable">$pageName</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'users'</span></span>
<span class="line">    <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="游标分页" tabindex="-1"><a class="header-anchor" href="#游标分页"><span>游标分页</span></a></h3>
<p>虽然 <code v-pre>paginate</code> 和 <code v-pre>simplePaginate</code> 使用 SQL「offset」 子句创建查询，但游标分页通过构造「where」子句来工作，这些子句比较查询中包含的有序列的值，提供所有可用的最有效的数据库性能 Laravel 的分页方法。这种分页方法特别适合大型数据集和「无限」滚动用户界面。</p>
<p>与基于偏移量的分页在分页器生成的 URL 的查询字符串中包含页码不同，基于游标的分页在查询字符串中放置一个「游标」字符串。游标是一个编码字符串，包含下一个分页查询应该开始分页的位置和它应该分页的方向：</p>
<div class="language-nothing line-numbers-mode" data-highlighter="prismjs" data-ext="nothing" data-title="nothing"><pre v-pre class="language-nothing"><code><span class="line">http://localhost/users?cursor=eyJpZCI6MTUsIl9wb2ludHNUb05leHRJdGVtcyI6dHJ1ZX0</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你可以通过查询构建器提供的 <code v-pre>cursorPaginate</code> 方法创建一个基于游标的分页器实例。此方法返回一个 <code v-pre>Illuminate\Pagination\CursorPaginator</code> 实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name static-context">DB</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">orderBy</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">cursorPaginate</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>一旦你获取了一个游标分页器实例，你可以像通常使用 <code v-pre>paginate</code> 和 <code v-pre>simplePaginate</code> 方法时那样<a href="#displaying-pagination-results">显示分页结果</a>。有关游标分页器提供的实例方法的更多信息，请参阅<a href="#cursor-paginator-instance-methods">游标分页器实例方法文档</a>。</p>
<blockquote>
<p>[!警告]<br>
为了利用游标分页，你的查询必须包含一个「order by」子句。此外，对查询进行排序的列必须属于你要分页的表。</p>
</blockquote>
<h4 id="游标分页与偏移分页" tabindex="-1"><a class="header-anchor" href="#游标分页与偏移分页"><span>游标分页与偏移分页</span></a></h4>
<p>为了说明偏移分页和游标分页之间的差异，让我们检查一些示例 SQL 查询。以下两个查询都将显示按 <code v-pre>id</code> 排序的 <code v-pre>users</code> 表的「第二页」结果：</p>
<div class="language-sql line-numbers-mode" data-highlighter="prismjs" data-ext="sql" data-title="sql"><pre v-pre class="language-sql"><code><span class="line"><span class="token comment"># 偏移分页...</span></span>
<span class="line"><span class="token keyword">select</span> <span class="token operator">*</span> <span class="token keyword">from</span> users <span class="token keyword">order</span> <span class="token keyword">by</span> id <span class="token keyword">asc</span> <span class="token keyword">limit</span> <span class="token number">15</span> <span class="token keyword">offset</span> <span class="token number">15</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment"># 游标分页...</span></span>
<span class="line"><span class="token keyword">select</span> <span class="token operator">*</span> <span class="token keyword">from</span> users <span class="token keyword">where</span> id <span class="token operator">></span> <span class="token number">15</span> <span class="token keyword">order</span> <span class="token keyword">by</span> id <span class="token keyword">asc</span> <span class="token keyword">limit</span> <span class="token number">15</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>游标分页查询相对于偏移分页提供以下优势：</p>
<ul>
<li>对于大数据集，如果「order by」列有被索引，游标分页将提供更好的性能。这是因为「offset」子句会扫描所有先前匹配的数据。</li>
<li>对于频繁写入的数据集，如果最近向用户当前查看的页面添加或删除了结果，偏移分页可能会跳过记录或显示重复项。</li>
</ul>
<p>然而，游标分页有以下限制：</p>
<ul>
<li>与 <code v-pre>simplePaginate</code> 类似，游标分页只能用于显示「下一页」和「上一页」链接，不支持生成带页码的链接。</li>
<li>它要求排序至少基于一个唯一列或一组唯一列。不支持带有 <code v-pre>null</code> 值的列。</li>
<li>「order by」子句中的查询表达式仅在它们被别名并添加到「select」子句中时才受支持。</li>
<li>带有参数的查询表达式不受支持。</li>
</ul>
<h3 id="手动创建分页器" tabindex="-1"><a class="header-anchor" href="#手动创建分页器"><span>手动创建分页器</span></a></h3>
<p>有时你可能希望手动创建一个分页器实例，传递一个你已经在内存中的条目数组。你可以根据需要创建一个 <code v-pre>Illuminate\Pagination\Paginator</code>、<code v-pre>Illuminate\Pagination\LengthAwarePaginator</code> 或 <code v-pre>Illuminate\Pagination\CursorPaginator</code> 实例。</p>
<p><code v-pre>Paginator</code> 和 <code v-pre>CursorPaginator</code> 类不需要知道结果集中的总条目数；然而，正因为如此，这些类没有用于检索最后一页编号的方法。<code v-pre>LengthAwarePaginator</code> 接受与 <code v-pre>Paginator</code> 几乎相同的参数；但是，它需要结果集中总条数的计数。</p>
<p>换句话说，<code v-pre>Paginator</code> 对应于查询构建器上的 <code v-pre>simplePaginate</code> 方法，<code v-pre>CursorPaginator</code> 对应于 <code v-pre>cursorPaginate</code> 方法，而 <code v-pre>LengthAwarePaginator</code> 对应于 <code v-pre>paginate</code> 方法。</p>
<blockquote>
<p>[!警告]<br>
当手动创建分页器实例时，你应该手动「切片」传递给分页器的数组结果。如果你不确定如何做到这一点，请查看 <a href="https://secure.php.net/manual/en/function.array-slice.php" target="_blank" rel="noopener noreferrer">array_slice</a> PHP 函数。</p>
</blockquote>
<h3 id="自定义分页-url" tabindex="-1"><a class="header-anchor" href="#自定义分页-url"><span>自定义分页 URL</span></a></h3>
<p>默认情况下，分页器生成的链接将匹配当前请求的 URI。然而，分页器的 <code v-pre>withPath</code> 方法允许你自定义分页器生成链接时使用的 URI。例如，如果你想让分页器生成类似 <code v-pre>http://example.com/admin/users?page=N</code> 的链接，你应该将 <code v-pre>/admin/users</code> 传递给 <code v-pre>withPath</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">paginate</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">withPath</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/admin/users'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="附加查询字符串值" tabindex="-1"><a class="header-anchor" href="#附加查询字符串值"><span>附加查询字符串值</span></a></h4>
<p>你可以使用 <code v-pre>appends</code> 方法将查询字符串附加到分页链接中。例如，要将 <code v-pre>sort=votes</code> 附加到每个分页链接，你应该进行以下调用：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">paginate</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">appends</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'sort'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'votes'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你想将当前请求的所有查询字符串值附加到分页链接中，可以使用 <code v-pre>withQueryString</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">paginate</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">withQueryString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="附加哈希片段" tabindex="-1"><a class="header-anchor" href="#附加哈希片段"><span>附加哈希片段</span></a></h4>
<p>如果你需要将「哈希片段」附加到分页器生成的 URL 中，可以使用 <code v-pre>fragment</code> 方法。例如，要将 <code v-pre>#users</code> 附加到每个分页链接的末尾，你应该这样调用 <code v-pre>fragment</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">paginate</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">fragment</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="显示分页结果" tabindex="-1"><a class="header-anchor" href="#显示分页结果"><span>显示分页结果</span></a></h2>
<p>当调用 <code v-pre>paginate</code> 方法时，你将收到一个 <code v-pre>Illuminate\Pagination\LengthAwarePaginator</code> 实例，而调用 <code v-pre>simplePaginate</code> 方法将返回一个 <code v-pre>Illuminate\Pagination\Paginator</code> 实例。最后，调用 <code v-pre>cursorPaginate</code> 方法将返回一个 <code v-pre>Illuminate\Pagination\CursorPaginator</code> 实例。</p>
<p>这些对象提供了几个描述结果集的方法。除了这些辅助方法外，分页器实例是迭代器，可以像数组一样循环。因此，一旦你获取到结果，你可以使用 <a href="https://learnku.com/docs/laravel/11.x/blade" target="_blank" rel="noopener noreferrer">Blade</a> 显示结果并渲染页面链接：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">&lt;div class=&quot;container&quot;&gt;</span>
<span class="line">    @foreach ($users as $user)</span>
<span class="line">        {{ $user-&gt;name }}</span>
<span class="line">    @endforeach</span>
<span class="line">&lt;/div&gt;</span>
<span class="line"></span>
<span class="line">{{ $users-&gt;links() }}</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>links</code> 方法将渲染结果集中其余页面的链接。每个链接都将包含正确的 <code v-pre>page</code> 查询字符串变量。记住，<code v-pre>links</code> 方法生成的 HTML 与 <a href="https://tailwindcss.com/" target="_blank" rel="noopener noreferrer">Tailwind CSS 框架</a> 兼容。</p>
<h3 id="调整分页链接窗口" tabindex="-1"><a class="header-anchor" href="#调整分页链接窗口"><span>调整分页链接窗口</span></a></h3>
<p>当分页器显示分页链接时，当前页码以及当前页之前和之后的三个页面的链接都会显示。使用 <code v-pre>onEachSide</code> 方法，你可以控制分页器生成的中间滑动窗口链接中，当前页两侧显示的额外链接数量：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">{{ $users-&gt;onEachSide(5)-&gt;links() }}</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="转换结果为-json" tabindex="-1"><a class="header-anchor" href="#转换结果为-json"><span>转换结果为 JSON</span></a></h3>
<p>Laravel 分页器类实现了 <code v-pre>Illuminate\Contracts\Support\Jsonable</code> 接口契约并暴露了 <code v-pre>toJson</code> 方法，因此将分页结果转换为 JSON 非常容易。你还可以通过从路由或控制器操作返回分页器实例来将其转换为 JSON：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">paginate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>分页器的 JSON 将包含元信息，如 <code v-pre>total</code>、<code v-pre>current_page</code>、<code v-pre>last_page</code> 等等。结果记录可通过 JSON 数组中的 <code v-pre>data</code> 键获得。以下是从路由返回分页器实例创建的 JSON 示例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">{</span></span>
<span class="line">   <span class="token string double-quoted-string">"total"</span><span class="token punctuation">:</span> <span class="token number">50</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"per_page"</span><span class="token punctuation">:</span> <span class="token number">15</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"current_page"</span><span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"last_page"</span><span class="token punctuation">:</span> <span class="token number">4</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"first_page_url"</span><span class="token punctuation">:</span> <span class="token string double-quoted-string">"http://laravel.app?page=1"</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"last_page_url"</span><span class="token punctuation">:</span> <span class="token string double-quoted-string">"http://laravel.app?page=4"</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"next_page_url"</span><span class="token punctuation">:</span> <span class="token string double-quoted-string">"http://laravel.app?page=2"</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"prev_page_url"</span><span class="token punctuation">:</span> <span class="token constant">null</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"path"</span><span class="token punctuation">:</span> <span class="token string double-quoted-string">"http://laravel.app"</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"from"</span><span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"to"</span><span class="token punctuation">:</span> <span class="token number">15</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"data"</span><span class="token punctuation">:</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token punctuation">{</span></span>
<span class="line">            <span class="token comment">// 记录...</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">{</span></span>
<span class="line">            <span class="token comment">// 记录...</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line">   <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="自定义分页视图" tabindex="-1"><a class="header-anchor" href="#自定义分页视图"><span>自定义分页视图</span></a></h2>
<p>默认情况下，用于显示分页链接的视图与 <a href="https://tailwindcss.com/" target="_blank" rel="noopener noreferrer">Tailwind CSS</a> 框架兼容。不过，如果你不使用 Tailwind，你可以自由定义自己的视图来渲染这些链接。在分页器实例上调用 <code v-pre>links</code> 方法时，你可以将视图名称作为方法的第一个参数传递：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">{{ $paginator-&gt;links('view.name') }}</span>
<span class="line"></span>
<span class="line">&lt;!-- 向视图传递额外数据… --&gt;</span>
<span class="line">{{ $paginator-&gt;links('view.name', ['foo' =&gt; 'bar']) }}</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>然而，自定义分页视图的最简单方法是使用 <code v-pre>vendor:publish</code> 命令将它们导出到 <code v-pre>resources/views/vendor</code> 目录：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan vendor:publish <span class="token parameter variable">--tag</span><span class="token operator">=</span>laravel-pagination</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>此命令会将视图放置在你应用程序的 <code v-pre>resources/views/vendor/pagination</code> 目录中。该目录中的 <code v-pre>tailwind.blade.php</code> 文件对应于默认的分页视图。你可以编辑此文件以修改分页 HTML。</p>
<p>如果你想指定一个不同的文件作为默认分页视图，可以在 <code v-pre>App\Providers\AppServiceProvider</code> 类的 <code v-pre>boot</code> 方法中调用分页器的 <code v-pre>defaultView</code> 和 <code v-pre>defaultSimpleView</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Providers</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Pagination<span class="token punctuation">\</span>Paginator</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>ServiceProvider</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">AppServiceProvider</span> <span class="token keyword">extends</span> <span class="token class-name">ServiceProvider</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 启动任何应用程序服务。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token class-name static-context">Paginator</span><span class="token operator">::</span><span class="token function">defaultView</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'view-name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token class-name static-context">Paginator</span><span class="token operator">::</span><span class="token function">defaultSimpleView</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'view-name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="使用-bootstrap" tabindex="-1"><a class="header-anchor" href="#使用-bootstrap"><span>使用 Bootstrap</span></a></h3>
<p>Laravel 包含使用 <a href="https://getbootstrap.com/" target="_blank" rel="noopener noreferrer">Bootstrap CSS</a> 构建的分页视图。要使用这些视图而不是默认的 Tailwind 视图，可以在 <code v-pre>App\Providers\AppServiceProvider</code> 类的 <code v-pre>boot</code> 方法中调用分页器的 <code v-pre>useBootstrapFour</code> 或 <code v-pre>useBootstrapFive</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Pagination<span class="token punctuation">\</span>Paginator</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 启动任何应用程序服务。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Paginator</span><span class="token operator">::</span><span class="token function">useBootstrapFive</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token class-name static-context">Paginator</span><span class="token operator">::</span><span class="token function">useBootstrapFour</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="paginator-lengthawarepaginator-实例方法" tabindex="-1"><a class="header-anchor" href="#paginator-lengthawarepaginator-实例方法"><span>Paginator / LengthAwarePaginator 实例方法</span></a></h2>
<p>每个分页器实例通过以下方法提供额外的分页信息：</p>
<table>
<thead>
<tr>
<th>方法</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><code v-pre>$paginator-&gt;count()</code></td>
<td>获取当前页的条目数。</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;currentPage()</code></td>
<td>获取当前页码。</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;firstItem()</code></td>
<td>获取结果中第一项的编号。</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;getOptions()</code></td>
<td>获取分页器选项。</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;getUrlRange($start, $end)</code></td>
<td>创建一个范围的分页 URL。</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;hasPages()</code></td>
<td>确定是否有足够的条目可以拆分为多个分页。</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;hasMorePages()</code></td>
<td>确定数据存储中是否有更多条目。</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;items()</code></td>
<td>获取当前页面的条目。</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;lastItem()</code></td>
<td>获取结果中最后一项的编号。</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;lastPage()</code></td>
<td>获取最后一页的页码。（使用 <code v-pre>simplePaginate</code> 时不可用）。</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;nextPageUrl()</code></td>
<td>获取下一页的 URL。</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;onFirstPage()</code></td>
<td>确定分页器是否在第一页。</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;perPage()</code></td>
<td>每页显示的条目数。</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;previousPageUrl()</code></td>
<td>获取前一页的 URL。</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;total()</code></td>
<td>确定数据存储中匹配条目的总数。（使用 <code v-pre>simplePaginate</code> 时不可用）。</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;url($page)</code></td>
<td>获取给定页码的 URL。</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;getPageName()</code></td>
<td>获取用于存储页码的查询字符串变量。</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;setPageName($name)</code></td>
<td>设置用于存储页码的查询字符串变量。</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;through($callback)</code></td>
<td>使用回调转换每个项。</td>
</tr>
</tbody>
</table>
<h2 id="游标分页器-cursor-paginator-实例方法" tabindex="-1"><a class="header-anchor" href="#游标分页器-cursor-paginator-实例方法"><span>游标分页器（Cursor Paginator）实例方法</span></a></h2>
<p>每个游标分页器实例通过以下方法提供额外的分页信息：</p>
<table>
<thead>
<tr>
<th>方法</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><code v-pre>$paginator-&gt;count()</code></td>
<td>获取当前页的条目数。</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;cursor()</code></td>
<td>获取当前游标实例。</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;getOptions()</code></td>
<td>获取分页器选项。</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;hasPages()</code></td>
<td>确定是否有足够的项目可以拆分为多个分页。</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;hasMorePages()</code></td>
<td>确定数据存储中是否有更多条目数据。</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;getCursorName()</code></td>
<td>获取用于存储游标的查询字符串变量。</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;items()</code></td>
<td>获取当前页面的条目数据。</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;nextCursor()</code></td>
<td>获取下一组条目的游标实例。</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;nextPageUrl()</code></td>
<td>获取下一页的 URL。</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;onFirstPage()</code></td>
<td>确定分页器是否在第一页。</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;onLastPage()</code></td>
<td>确定分页器是否在最后一页。</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;perPage()</code></td>
<td>每页显示的条目数。</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;previousCursor()</code></td>
<td>获取前一组条目的游标实例。</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;previousPageUrl()</code></td>
<td>获取前一页的 URL。</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;setCursorName()</code></td>
<td>设置用于存储游标的查询字符串变量。</td>
</tr>
<tr>
<td><code v-pre>$paginator-&gt;url($cursor)</code></td>
<td>获取给定游标实例的 URL。</td>
</tr>
</tbody>
</table>
<blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/11.x/paginationmd/16698" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/pa...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/11.x/paginationmd/16698" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/pa...</a></p>
</blockquote>
</div></template>


